//
//  FFBankcardAddInputCell.m
//  LLWallet
//
//  Created by tt on 2020/9/14.
//

#import "FFBankcardAddInputCell.h"
#import <Masonry/Masonry.h>

@implementation FFBankcardAddInputModel

- (Class)cell {
    return FFBankcardAddInputCell.class;
}

@end

@interface FFBankcardAddInputCell ()<UITextFieldDelegate>

@property(nonatomic,strong) UILabel *labelLbl;
@property(nonatomic,strong) UITextField *inputFd;
@property(nonatomic,strong) FFBankcardAddInputModel *model;

@end

@implementation FFBankcardAddInputCell

+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 90.0f);
}

- (void)setupUI {
    [super setupUI];
    
    [self.contentView addSubview:self.labelLbl];
    [self.contentView addSubview:self.inputFd];
    
}

- (void)refresh:(FFBankcardAddInputModel *)model {
    [super refresh:model];
    self.model = model;
    self.labelLbl.text = model.label;
    self.inputFd.placeholder = model.placeholder;
    self.inputFd.text = model.value;
    self.inputFd.enabled = !model.disable;
    self.inputFd.delegate = self;
    if(model.keyboardType) {
        self.inputFd.keyboardType =model.keyboardType.integerValue;
    }else{
        self.inputFd.keyboardType = UIKeyboardTypeDefault;
    }
    
}

- (UILabel *)labelLbl {
    if(!_labelLbl) {
        _labelLbl = [[UILabel alloc] init];
        [_labelLbl setFont:[[WKApp shared].config appFontOfSize:12.0f]];
        [_labelLbl setTextColor:[WKApp shared].config.tipColor];
    }
    return _labelLbl;
}

- (UITextField *)inputFd {
    if(!_inputFd) {
        _inputFd = [[UITextField alloc] init];
        [_inputFd addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inputFd;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.labelLbl sizeToFit];
    self.labelLbl.lim_top = 20.0f;
    self.labelLbl.lim_left = 15.0f;
    
    self.inputFd.lim_top = self.labelLbl.lim_bottom + 10.0f;
    self.inputFd.lim_height = 30.0f;
    self.inputFd.lim_left = self.labelLbl.lim_left;
    self.inputFd.lim_width = self.lim_width - self.inputFd.lim_left*2;
    
    self.bottomLineView.lim_left = self.labelLbl.lim_left;
    self.bottomLineView.lim_width = self.lim_width - self.bottomLineView.lim_left * 2.0f;
    
}

-(void) textChange:(UITextField *)textField {
    if(self.model.maxLen && self.model.maxLen.integerValue > 0 ) {
        NSString *toBeString = textField.text;
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        CGFloat maxLength = self.model.maxLen.floatValue;
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position){
            if (toBeString.length > maxLength){
                
                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
                if (rangeIndex.length == 1){
                    
                    textField.text = [toBeString substringToIndex:maxLength];
                }
                else{
                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                    textField.text = [toBeString substringWithRange:rangeRange];
                }
            }
        }
    }
    if(self.model.onChange) {
        self.model.onChange(textField.text);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.inputFd && self.model.isBankcard) {
        if(textField.text.length == 0) {
            return YES;
        }
        // 四位加一个空格
        if ([string isEqualToString:@""]) { // 删除字符
            if(textField.text.length == 1) {
                return YES;
            }
            if ((textField.text.length - 1) % 5 == 0) {
                textField.text = [textField.text substringToIndex:textField.text.length - 1];
            }
            return YES;
        } else {
            if ((textField.text.length+string.length) % 5 == 0) {
                textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
            }
        }
        return YES;
    }
    return YES;
}

@end

@implementation WKAddressAddInputModel

- (Class)cell {
    return WKAddressAddInputCell.class;
}

@end


@interface WKAddressAddInputCell ()<UITextViewDelegate>

@property(nonatomic,strong) WKAddressAddInputModel *model;

@end


@implementation WKAddressAddInputCell

+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 130.0f);
}

- (void)setupUI {
    [super setupUI];
    
    [self.contentView addSubview:self.labelLbl];
    [self.contentView addSubview:self.inputFd];
    
}

- (UILabel *)labelLbl {
    if(!_labelLbl) {
        _labelLbl = [[UILabel alloc] init];
        [_labelLbl setFont:[[WKApp shared].config appFontOfSize:16.0f]];
        [_labelLbl setTextColor:[WKApp shared].config.defaultTextColor];
    }
    return _labelLbl;
}

- (UITextView *)inputFd {
    if(!_inputFd) {
        _inputFd = [[UITextView alloc] init];
        _inputFd.delegate = self;
        _inputFd.font = [[WKApp shared].config appFontOfSize:15.0f];
//        [_inputFd addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inputFd;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.labelLbl sizeToFit];
    self.labelLbl.lim_top = 20.0f;
    self.labelLbl.lim_left = 15.0f;
    
    self.inputFd.lim_top = self.labelLbl.lim_bottom + 10.0f;
    self.inputFd.lim_height = 80.0f;
    self.inputFd.lim_left = self.labelLbl.lim_left;
    self.inputFd.lim_width = self.lim_width - self.inputFd.lim_left*2;
    
    self.bottomLineView.lim_left = self.labelLbl.lim_left;
    self.bottomLineView.lim_width = self.lim_width - self.bottomLineView.lim_left * 2.0f;
    
}
- (void)refresh:(WKAddressAddInputModel *)model {
    [super refresh:model];
    self.model = model;
    self.labelLbl.text = model.label;
    self.inputFd.placeholder = model.placeholder;
}
- (void)textViewDidChange:(UITextView *)textView{
    if(self.model.onChange) {
        self.model.onChange(textView.text);
    }
}
@end


@implementation WKCoinModel

- (Class)cell {
    return WKCoinCell.class;
}

@end


@interface WKCoinCell ()

@property(nonatomic,strong) WKCoinModel *model;

@end


@implementation WKCoinCell

+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 80);
}

- (void)setupUI {
    [super setupUI];
    
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.desLab];
    [self.contentView addSubview:self.logo];
    
    [self.logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(50);
    }];
    
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.left.mas_equalTo(self.logo.mas_right).offset(12);
    }];
    
    [self.desLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-16);
        make.left.mas_equalTo(self.logo.mas_right).offset(12);
    }];
}

- (UILabel *)nameLab {
    if(!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        [_nameLab setFont:[[WKApp shared].config appFontOfSize:16.0f]];
        [_nameLab setTextColor:[WKApp shared].config.defaultTextColor];
    }
    return _nameLab;
}
- (UILabel *)desLab {
    if(!_desLab) {
        _desLab = [[UILabel alloc] init];
        [_desLab setFont:[[WKApp shared].config appFontOfSize:14.0f]];
        [_desLab setTextColor:[UIColor grayColor]];
    }
    return _desLab;
}

- (UIImageView *)logo{
    if (!_logo){
        _logo = [UIImageView new];
    }
    return _logo;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
}
- (void)refresh:(WKCoinModel *)model {
    [super refresh:model];
    self.model = model;
    self.nameLab.text = model.name;
    self.desLab.text = model.des;
    [self.logo sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:nil];
}

@end
